package com.sykj.cloud.system.controller;

import com.sykj.cloud.common.core.utils.poi.ExcelUtil;
import com.sykj.cloud.common.core.web.controller.BaseController;
import com.sykj.cloud.common.core.web.domain.AjaxResult;
import com.sykj.cloud.common.core.web.page.TableDataInfo;
import com.sykj.cloud.common.log.annotation.Log;
import com.sykj.cloud.common.log.enums.LogUserType;
import com.sykj.cloud.common.security.annotation.PreAuthorize;
import com.sykj.cloud.system.api.domain.SysRole;
import com.sykj.cloud.system.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 系统角色Controller
 *
 * @author mr.will
 * @date 2021-03-17
 */
@RestController
@RequestMapping("/role")
public class SysRoleController extends BaseController
{
    @Autowired
    private ISysRoleService sysRoleService;


    /**
     * 新增系统角色
     */
    @PreAuthorize(hasPermi = "system:role:add")
    @Log(title = "系统角色", logUserType = LogUserType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysRole sysRole)
    {
        return toAjax(sysRoleService.insertSysRole(sysRole));
    }

    /**
     * 修改系统角色
     */
    @PreAuthorize(hasPermi = "system:role:edit")
    @Log(title = "系统角色", logUserType = LogUserType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysRole sysRole)
    {
        return toAjax(sysRoleService.updateSysRole(sysRole));
    }


    /**
     * 修改保存数据权限
     */
    @PreAuthorize(hasPermi = "system:role:edit")
    @Log(title = "角色管理", logUserType = LogUserType.UPDATE)
    @PutMapping("/dataScope")
    public AjaxResult dataScope(@RequestBody SysRole role)
    {
        sysRoleService.checkRoleAllowed(role);
        return toAjax(sysRoleService.authDataScope(role));
    }


    /**
     * 删除系统角色
     */
    @PreAuthorize(hasPermi = "system:role:remove")
    @Log(title = "系统角色", logUserType = LogUserType.DELETE)
	@DeleteMapping("/{guids}")
    public AjaxResult remove(@PathVariable String[] guids)
    {
        return toAjax(sysRoleService.deleteSysRoleByIds(guids));
    }


    /**
     * 获取系统角色详细信息
     */
    @PreAuthorize(hasPermi = "system:role:query")
    @GetMapping(value = "/{guid}")
    public AjaxResult getInfo(@PathVariable("guid") String guid)
    {
        return AjaxResult.success(sysRoleService.getSysRoleById(guid));
    }

    /**
     * 导出系统角色列表
     */
    @PreAuthorize(hasPermi = "system:role:export")
    @Log(title = "系统角色", logUserType = LogUserType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysRole sysRole) throws IOException
    {
        List<SysRole> list = sysRoleService.listSysRoles(sysRole);
        ExcelUtil<SysRole> util = new ExcelUtil<SysRole>(SysRole.class);
        util.exportExcel(response, list, "role");
    }


    /**
     * 查询系统角色列表
     */
    @PreAuthorize(hasPermi = "system:role:list")
    @GetMapping("/list")
    public TableDataInfo list(SysRole sysRole)
    {
        startPage();
        List<SysRole> list = sysRoleService.listSysRoles(sysRole);
        return getDataTable(list);
    }

}
